package com.zxw.malldubbo.ServiceImpl;

import com.zxw.malldubbo.dao.CategoryDao;
import jakarta.annotation.Resource;
import model.Category;
import org.apache.dubbo.auth.v1alpha1.Ca;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import service.CategoryService;

import java.util.*;

@DubboService
@Service
@CacheConfig(cacheNames = "com.zxw.malldubbo.ServiceImpl.CategoryServiceImpl")
public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao;

    @Autowired
    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    //查询全部
    @Override
    @Cacheable(keyGenerator = "myKeyGenerator")
    public List<Category> findAll() {
        System.out.println("sadasdasdas");
        return categoryDao.findAll();
    }

    //查询一棵树
    @Override
    @Cacheable(keyGenerator = "myKeyGenerator")
    public Category findtree() {
        //使用动态代理
        CategoryService cs = (CategoryService) AopContext.currentProxy();
        List<Category> categories = cs.findAll();
        return maketree(categories);
    }


    public static Category maketree(List<Category> categories) {
        //创建一个MAP
        Map<Integer, Category> map = new HashMap<>();
        //将数据添加到map
        categories.forEach(category -> map.put(category.getId(), category));
        //创建一个根数组
        List<Category> roots = new ArrayList<>();
        for (Category c : categories) {
            //得到根的集合
            if (c.getParentId() == null) {
                roots.add(c);
            } else {
                //获取有父节点的父
                Category parent = map.get(c.getParentId());
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(c);
            }
        }

            Category root = new Category();
            root.setId(0);
            root.setName("root");
            root.setChildren(new ArrayList<>());
            for (Category c : roots) {
                root.getChildren().add(c);
            }
            return root;



    }

    @Override
    @Cacheable(keyGenerator = "myKeyGenerator")
    public Category findById(Integer id) {
        return categoryDao.findById(id);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean save(Category category) {
        return categoryDao.save(category) > 0;
    }
    //根据id找父名字
    @Override
    public String findname(Integer id) {
        String getname = categoryDao.getname(id);
        return getname;
    }
    @Override
    @CacheEvict(allEntries = true)
    public boolean update(Category category) {
        return categoryDao.update(category) > 0;
    }

    @Override
    @CacheEvict(allEntries = true)
    public int deleteById(Integer id) {
        List<String> getproductname = categoryDao.getproductname(id);
        if(getproductname.isEmpty()){

            return categoryDao.deleteById(id);
        }else{
            return 0;
        }

    }

    @Override
    public Category tiaojian(Category category) {
        System.out.println(category.getDescription());
        //获取代理
        CategoryService cs = (CategoryService) AopContext.currentProxy();
        //得到所有的节点
        List<Category> categories = cs.findAll();
        System.out.println(category.getDescription());
        System.out.println(category.getName());
        // 从树里根据条件查询
       return tjtree(categories, category);
    }



    private Category tjtree(List<Category> categories, Category category) {
        String name = category.getName();

        String description = category.getDescription();

        //名字为空 描述不为空
       if ((name == null||name=="") && (description!=null||description!="")) {
            //根据描述查询
            List<Category> list1 = categories.stream().filter(c -> c.getDescription().contains(description)).toList();
            List<Category> list = new ArrayList<>(list1);
            //创建个新的集合 将list中的数据添加到新集合中
            List<Category> newList = new ArrayList<>(list1);
            List<Category> newList1 = new ArrayList<>(list1);
            List<Category> newList2 = new ArrayList<>(list1);
            int length = 0;
            do {
                length = newList.size();
                //根据新集合中的每个parentid从categories中查询并添加到新集合中 父
                for (Category c : newList) {
                    if (c.getParentId() != null) {
                        for (Category a : categories) {
                            if (Objects.equals(a.getId(), c.getParentId())) {
                                if (!newList1.contains(a)) {
                                    newList1.add(a);

                                }
                            }
                        }
                    }
                }
            } while (length != newList.size());
            int len = 0;
            do {
                len = list.size();
                //根据list中的每个id从categories中查询并添加到list中 子
                for (Category a : list) {
                    for (Category b : categories) {
                        if (Objects.equals(a.getId(), b.getParentId())) {
                            if (!newList2.contains(b)) {
                                newList2.add(b);
                            }
                        }
                    }
                }
            } while (len != list.size());
            //去重
            for (Category item : newList1) {
                if (!newList2.contains(item)) {
                    newList2.add(item);
                }
            }
            //合并两个集合去重
            return maketree(newList2);

        }
        //名字不为空 描述为空
        if ((name != null||name!="") && (description==""|| description==null)) {
            //根据描述查询
            List<Category> list1 = categories.stream().filter(c -> c.getName().contains(name)).toList();
            List<Category> list = new ArrayList<>(list1);
            //创建个新的集合 将list中的数据添加到新集合中
            List<Category> newList = new ArrayList<>(list1);
            List<Category> newList1 = new ArrayList<>(list1);
            List<Category> newList2 = new ArrayList<>(list1);
            int length = 0;
            do {
                length = newList.size();
                //根据新集合中的每个parentid从categories中查询并添加到新集合中 父
                for (Category c : newList) {
                    if (c.getParentId() != null) {
                        for (Category a : categories) {
                            if (Objects.equals(a.getId(), c.getParentId())) {
                                if (!newList1.contains(a)) {
                                    newList1.add(a);

                                }
                            }
                        }
                    }
                }
            } while (length != newList.size());
            int len = 0;
            do {
                len = list.size();
                //根据list中的每个id从categories中查询并添加到list中 子
                for (Category a : list) {
                    for (Category b : categories) {
                        if (Objects.equals(a.getId(), b.getParentId())) {
                            if (!newList2.contains(b)) {
                                newList2.add(b);
                            }
                        }
                    }
                }
            } while (len != list.size());
            //去重
            for (Category item : newList1) {
                if (!newList2.contains(item)) {
                    newList2.add(item);
                }
            }
            //合并两个集合去重
            return maketree(newList2);
        }
        //名字不为空 描述不为空
        if ((name != null||name!="") &&(description!=null||description!="")) {
            //根据描述查询
            List<Category> list1 = categories.stream().filter(c -> c.getName().contains(name)&&c.getDescription().contains(description)).toList();
            List<Category> list = new ArrayList<>(list1);
            //创建个新的集合 将list中的数据添加到新集合中
            List<Category> newList = new ArrayList<>(list1);
            List<Category> newList1 = new ArrayList<>(list1);
            List<Category> newList2 = new ArrayList<>(list1);
            int length = 0;
            do {
                length = newList.size();
                //根据新集合中的每个parentid从categories中查询并添加到新集合中 父
                for (Category c : newList) {
                    if (c.getParentId() != null) {
                        for (Category a : categories) {
                            if (Objects.equals(a.getId(), c.getParentId())) {
                                if (!newList1.contains(a)) {
                                    newList1.add(a);

                                }
                            }
                        }
                    }
                }
            } while (length != newList.size());
            int len = 0;
            do {
                len = list.size();
                //根据list中的每个id从categories中查询并添加到list中 子
                for (Category a : list) {
                    for (Category b : categories) {
                        if (Objects.equals(a.getId(), b.getParentId())) {
                            if (!newList2.contains(b)) {
                                newList2.add(b);
                            }
                        }
                    }
                }
            } while (len != list.size());
            //去重
            for (Category item : newList1) {
                if (!newList2.contains(item)) {
                    newList2.add(item);
                }
            }
            //合并两个集合去重
            return maketree(newList2);
        }
return null;
    }


}
